/*
 * @lc app=leetcode.cn id=234 lang=javascript
 *
 * [234] 回文链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function(head) {
    let queue=[]
    while(head){
        queue.push(head.val)
        head=head.next
    }
    let temp=[...queue]
    return queue.reverse().toString()===temp.toString()
};
// @lc code=end


head={val:1,next:{val:2,next:null}}
isPalindrome(head)
var isPalindrome = function(head) {
    let slow = head;
    let fast = head;
    let prev = null;
    let nextTmp;
    while (fast !== null && fast.next !== null) {
        fast = fast.next.next;
        nextTmp = slow.next;//快慢指针找中点
        slow.next = prev;
        prev = slow;//链表反转好比较
        slow = nextTmp;
    }
    if (fast !== null) slow = slow.next; // 奇数
    while (slow !== null) {
        if (prev.val !== slow.val) return false;
        slow = slow.next;
        prev = prev.next;
    }
    return true;
};//快慢指针加链表反转